# Maintainer: Sarah Ottinger <schalaalexiazeal@gmail.com>

_realname=onednn
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.6.2
pkgrel=1
pkgdesc='oneAPI Deep Neural Network Library (oneDNN) (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/oneapi-src/oneDNN"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-uasm"))
source=("https://github.com/oneapi-src/oneDNN/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('e79db0484dcefe2c7ff6604c295d1de2830c828941898878c80dfb062eb344d1')

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  if [[ ${CARCH} == x86_64 ]]; then
    extra_config+=('-DCMAKE_ASM_MASM_COMPILER="uasm;-win64"')
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${extra_config[@]}" \
    -DDNNL_BUILD_EXAMPLES=OFF \
    -DDNNL_BUILD_TESTS=OFF \
    ../oneDNN-$pkgver

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="$pkgdir" ${MINGW_PREFIX}/bin/cmake --install .

  install -D -m644 "${srcdir}"/oneDNN-${pkgver}/LICENSE \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
